#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "configfile.h"
#include "setbutton.h"
#include <QPainter>


//保存配置
void SaveConfigFile(QString group,QString choice,QString value)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);

    // 2. 写入字符串到指定Section和Key
    settings.beginGroup(group);       // 进入[UserInfo]节
    if(value=="font")
        settings.setValue(choice,qApp->font().toString());
    else
        settings.setValue(choice,value);
    settings.endGroup();
    //settings.sync();
}

//加载配置
void LoadConfigFile(Ui::MainWindow *ui,MainWindow *parent,QAudioOutput *audioOutput)
{
    FontCfg(ui);
    LrcCfg(ui);
    BackgroundCfg(parent);
    SoundCfg(audioOutput,ui);
    //QString MusicLoadCfg(QString DirPath);
}


//文字配置
void FontCfg(Ui::MainWindow *ui)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("FontSet");
    //字体样式
    QFont font;
    QString fontstyle=settings.value("fontstyle").toString();
    font.fromString(fontstyle);
    FontSet(font,ui);
    settings.endGroup();
}
//歌词配置
void LrcCfg(Ui::MainWindow *ui)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("LrcSet");
    bool lrc_isShow=settings.value("lrcShow").toBool();
    if(lrc_isShow)
    {
        ui->lyric_display->show();
        ui->song_title->show();
    }
    else
    {
        ui->lyric_display->hide();
        ui->song_title->hide();
    }
    settings.endGroup();
}
//背景配置
void BackgroundCfg(MainWindow *parent)
{

    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("BackGroundSet");
    QString imagePath=settings.value("imagepath").toString();
    settings.endGroup();



    QPixmap pixmap(imagePath);

    // QImage image = pixmap.toImage();
    // // 确保图像格式支持透明度
    // image = image.convertToFormat(QImage::Format_ARGB32);

    // // 亮度调整范围：-255（完全变暗）到 255（完全变亮）
    // double factor = -20 / 255.0;

    // for (int y = 0; y < image.height(); ++y) {
    //     QRgb *scanLine = reinterpret_cast<QRgb*>(image.scanLine(y));
    //     for (int x = 0; x < image.width(); ++x) {
    //         QRgb pixel = scanLine[x];

    //         int r = qRed(pixel);
    //         int g = qGreen(pixel);
    //         int b = qBlue(pixel);
    //         int a = qAlpha(pixel);

    //         // 应用亮度调整（简单线性调整）
    //         r = qBound(0, r + static_cast<int>(r * factor), 255);
    //         g = qBound(0, g + static_cast<int>(g * factor), 255);
    //         b = qBound(0, b + static_cast<int>(b * factor), 255);

    //         scanLine[x] = qRgba(r, g, b, a);
    //     }
    // }
    // pixmap=QPixmap::fromImage(image);
    //获得窗口尺寸
    QSize windowsize =parent->size();
    //照片缩放
    QPixmap scalePixmap=pixmap.scaled(windowsize,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

    QPixmap transparentPixmap(scalePixmap.size());
    transparentPixmap.fill(Qt::transparent);

    // 使用QPainter绘制带透明度的图片
    QPainter painter(&transparentPixmap);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.drawPixmap(0, 0, scalePixmap);
    painter.end();

    //创建pattle,用来放置图片
    QPalette palette =parent->palette();
    palette.setBrush(QPalette::Window,QBrush(scalePixmap));
    parent->setPalette(palette);
}
//声音设置
void SoundCfg(QAudioOutput *audioOutput,Ui::MainWindow *ui)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("Sound");
    //音量设置
    audioOutput->setVolume(settings.value("volume").toFloat());
    //静音设置
    audioOutput->setMuted(settings.value("muted").toBool());
    qDebug()<<audioOutput->isMuted()<<"+s+++";
    if(audioOutput->isMuted())
    {
        ui->mute_btn->setChecked(true);
    }
    int muted=audioOutput->isMuted();
    ui->mute_btn->setText(muted ? "取消静音":"静音");
    ui->sound_value->setText("音量:"+QString::number(audioOutput->volume()*100)+"%");



}

QString MusicLoadCfg(QString DirPath,bool *load_music,bool *play_music)
{
    QSettings settings(QDir::currentPath()+QDir::separator()+"config.ini", QSettings::IniFormat);
    settings.beginGroup("Music");
    DirPath=settings.value("defaultpath").toString();
    *load_music=settings.value("isload").toBool();
    *play_music=settings.value("isplay").toBool();
    settings.endGroup();
    return DirPath;
}
